import axios from 'axios'
import BASEURL from './BaseUrl'
// import store from '@/store/showtoast/index';
import store from "@/store";
import Cookies from 'js-cookie'
// const aysncchanisshowtoast=store.actions.aysncchanisshowtoast
const { URL } = BASEURL
const instance = axios.create({
    timeout: 5000
})
instance.interceptors.request.use(config => {
    // console.log("请求拦截器config:", config);
    // const token =localStorage.getItem('x-auth-token')
    // if(token){
    //     config.headers['x-auth-token']=token
    // }
    // http://kumanxuan1.f3322.net:8881/cms/sendSMS
    if (config.url === URL + "/sendSMS" || config.url === URL + "/wechatUsers/PCLogin") {
        config.headers["Content-Type"] = "application/x-www-form-urlencoded";
    }

    let myToken = Cookies.get("x-auth-token");
    // let myToken = localStorage.getItem("x-auth-token");
    if (myToken) {
        //存在就携带上
        config.headers["x-auth-token"] = myToken;
    }
    return config
}), err => {
    return Promise.reject(err)
}
instance.interceptors.response.use(res => {
    let res_data = res.data
    if (res_data.code != 0 && res_data.code != 400 && res_data.code != 407) {
        store.dispatch("showtoast/aysncchanisshowtoast", {
            msg: res_data.message,
            type: "success"
          });
        return
    }
    return res_data
}), err => {
    store.dispatch("showtoast/aysncchanisshowtoast", {
        msg: err,
        type: "success"
      });
    return Promise.reject(err)
}
export default instance

